package com.pine.sunshineelder.bed.web.handler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pine.sunshineelder.bed.ao.BedAo;
import com.pine.sunshineelder.bed.service.BedService;
import com.pine.sunshineelder.common.StringHelper;
import com.pine.sunshineelder.common.dto.JSONResponse;
import com.pine.sunshineelder.common.dto.QueryResult;
import com.pine.sunshineelder.common.entity.BedEntity;

/**
 * 
 * @author 卜涛元
 *
 */
@RestController
@RequestMapping("/api/bed")
public class BedHandler implements StringHelper{
	@Autowired
	BedService service;
	
	@PostMapping("/add")
	public JSONResponse registerBed(BedAo ao) throws Exception {
		BedEntity bed = service.addBed(ao);
		
 
		return JSONResponse.ok(bed);
	}
	
	@GetMapping("/{id}")
	public JSONResponse selectByBedId(@PathVariable("id") String id) throws Exception {
		BedEntity bedId = service.selectById(id);

		return JSONResponse.ok(bedId);
	}
	
	@GetMapping("/")
	public JSONResponse selectByState( String state) throws Exception {
		List<BedEntity> bedState = service.selectByState(state);
        

		return JSONResponse.ok(bedState);
	}
	
	
	@PutMapping("/mod/{id}")
	public JSONResponse modifyUser(@PathVariable String id, BedAo ao) throws Exception {
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("no", ao.getNo());
		map.put("state", ao.getState());
		map.put("description", ao.getDescription());
		System.out.println(map);
		service.modifyBed(id, map);
		return JSONResponse.ok();
	}
	
	@GetMapping("/list")
	public JSONResponse listBed(String key, String pageNo, String pageSize) throws Exception {
//		key = key.trim();
		if (key=="") key = null;
		Integer pageNoUse = parseInt(pageNo, null);
		Integer pageSizeUse = parseInt(pageSize, null);
		QueryResult result = service.queryByKey(key, pageNoUse, pageSizeUse);
		return JSONResponse.ok(result);
	}
	
	
}
